﻿<%
class UserClass
    public property get CACHE_PREFIX 
        CACHE_PREFIX = "USER"
    end property

    private m_userId  
    public userName ,  email , webUrl, faceUrl, msn, qq, sign, aliases
    private m_Blog
    private m_password
    
    private sub class_initialize()
       set m_blog = new BlogClass
    end sub
    
    private sub class_terminate()
    end sub
    
    public Property get blog
        set blog = m_Blog
    end Property
    
    public Property get password
        password = m_password
    end Property
    
    public Property let password(pwd)
        m_password = pwd
    end Property

    public Property get userId
        userId = m_userId
    end Property    

    public function loadById(userid)
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")
        call openDB
        loadById = false
        if not isEmpty(userid) and userid<>"" then
            sql = "select " & COL_USER_NAME & " from " & TBL_USER & " where " & COL_USER_ID & "="&userid
            rs.open sql,userConn,1,1
            if not rs.bof and not rs.eof then
                userName = rs(COL_USER_NAME)
                rs.close
                loadById = load(userName)
            else
                rs.close    
            end if
        end if                
        set rs = nothing
    end function
    
    public function load(user_name)
        load = false
        if isEmpty(user_name) then
            exit function
        end if
        userName = filterSQL(user_name)

        load = loadFromCache
        if load=false then
            load = loadFromDB()
            if load=true then
                call cache()
            end if    
        end if    
        if isEmpty(aliases) then
            redim aliases(0)
        end if
    end function
    
    public function reload(user_name)
        reload = false
        if isEmpty(user_name) then
            exit function
        end if

        userName = filterSQL(user_name)
        
        reload = loadFromDB()
        if (reload=true) then
            call cache()
        else    
            call clean()
        end if    
    end function

    public function clean()
        call removeFromCache(CACHE_PREFIX, userName)
        m_userId = Empty
        userName = Empty
        m_password = Empty
        email = Empty
        webUrl = Empty
        faceUrl = Empty
        msn = Empty
        qq = Empty
        sign = Empty
        redim aliases(0)
        set m_Blog = Nothing
        m_blog = Empty
    end function
    
    
    public sub cache()
        if not isEmpty(m_userId) then
            call build(putIntoCache(CACHE_PREFIX, userName, toArray()))
        end if    
    end sub
    

    private function loadFromCache()
        loadFromCache = build(getFromCache(CACHE_PREFIX, userName))
    end function
    
    public function build(array)
        build = false
        if isEmpty(array) or not isArray(array) then
            exit function
        end if
        if ubound(array)<11 then
            exit function
        end if
                
        m_userId = array(0)
        userName = array(1)
        m_password = array(2)
        email = array(3)
        webUrl = array(4)
        faceUrl = array(5)
        msn = array(6)
        qq = array(7)
        sign = array(8)
        aliases = array(9)
        m_Blog.load(array(10))
        build = true 
    end function 
        
    public function toArray()
        dim array(11)    
        array(0) = m_userId
        array(1) = userName 
        array(2) = m_password
        array(3) = email 
        array(4) = webUrl
        array(5) = faceUrl 
        array(6) = msn 
        array(7) = qq 
        array(8) = sign 
        array(9) = aliases 
        array(10) = m_Blog.blogId
        
        toArray = array
    end function


    private function loadFromDB()
        'response.write "loadFromDB"
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

        loadFromDB = false
        call openDB

        if userConn = conn then
            sql = " select a.*, b.alias   from " & TBL_USER & " a " &_
                  " left outer join user_alias b on a." & COL_USER_ID & "=b.userid " &_
                  " where a." & COL_USER_NAME & " ='" & userName & "' or b.alias='" & userName &"' " 
        else
            sql = " select a.* from " & TBL_USER & " a " &_
                  " where a." & COL_USER_NAME & " ='" & userName & "'  " 
        end if    
        'response.write sql
        'response.end
        rs.open sql, userConn, 1, 1
        if rs.eof  then
            loadFromDB = false
            rs.close()
        else    
            on error resume next            
            
            m_userId = rs(COL_USER_ID)          
            m_password = rs(COL_USER_PASSWORD)  
            email = rs(COL_USER_EMAIL)          
            webUrl = rs(COL_USER_WEB_URL)       
            faceUrl = rs(COL_USER_FACE_URL)     
            msn = rs(COL_USER_MSN)              
            qq = rs(COL_USER_QQ)                
            sign = rs(COL_USER_SIGN)             
                                                
            if userConn = conn then
                rs.close()
                sql = " select alias  from user_alias where " & COL_USER_ID & " = " &  m_userId
                rs.open sql, userConn, 1, 1
                dim count ,i                        
                count = rs.recordCount
                redim aliases(count)
                for i=0 to count-1
                    aliases(i) = rs("alias")
                    rs.moveNext
                next            
            end if      
            rs.close()
            
            on error goto 0      
            
                        
            loadFromDB = true    
            
            dim blog_id
            sql = " select blog_id from blog where user_id=" & m_userId  
            rs.open sql, conn, 1, 1
            if not rs.eof then
                blog_id = rs("blog_id")
                rs.close()
                m_blog.load(blog_id) 
            else
                rs.close()    
            end if         
            
        end if    
        set rs = nothing
        
    end function    
    
    public function store()
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")
        call openDB
        
        dim tmpUserObj
        set tmpUserObj = new UserClass
        tmpUserObj.load(userName)
        
        sql = " select * from " & TBL_USER
        if not tmpUserObj.isFound then
            sql = sql & " where " & COL_USER_ID & " =1 "
            rs.open sql, userConn, 1, 3
            rs.addNew()
            rs(COL_USER_NAME)=userName
        elseif tmpUserObj.userId<>userId then
            call closeDB
            message("用户名<a href=user.asp?user_id=" & tmpUserObj.userId &">" & tmpUserObj.userName & "</a>已经被其他用户注册")
            exit function
        else
            '重新加载别名
            aliases = tmpUserObj.aliases
            sql = sql & " where " & COL_USER_ID & " =" & userId
            rs.open sql, userConn, 1, 3
        end if
        
        rs(COL_USER_PASSWORD)=password

        if not isEmpty(faceUrl) and faceUrl<>"" then
            rs(COL_USER_FACE_URL)= faceUrl
        else
            rs(COL_USER_FACE_URL)= null
        end if    

        if not isEmpty(msn) and msn<>"" then
            rs(COL_USER_MSN)= msn
        else
            rs(COL_USER_MSN)= null
        end if    

        if not isEmpty(webUrl) and webUrl<>"" then
            rs(COL_USER_WEB_URL)= webUrl
        else
            rs(COL_USER_WEB_URL)= null
        end if    
        
        if not isEmpty(email) and email<>"" then
            rs(COL_USER_EMAIL)= email
        else
            rs(COL_USER_EMAIL)= null
        end if        
        
        if not isEmpty(qq) and qq<>"" then
            rs(COL_USER_QQ)= clng(qq)
        else    
            rs(COL_USER_QQ)= null
        end if
        
        if not isEmpty(sign) and sign<>"" then
            rs(COL_USER_SIGN)= sign
        else
            rs(COL_USER_SIGN)= null
        end if    

        rs.update()
        m_userId = rs(COL_USER_ID)
        
        rs.close()
        set rs = nothing

        set tmpUserObj = nothing
        
        call cache()
    end function
    
    public function isFound
        if not isEmpty(m_userId) and not isNull(m_userId) and m_userId<>"" then
            isFound = true
        else
            isFound=false
        end if
    end function

    public function isAdmin
      isAdmin = false  
      if not isEmpty(userName) and not isNull(userName) then
        if lcase(userName)="admin" and isFound then
            isAdmin = true
        end if    
      end if  
    end function

end class

%>